<template>
  <div class="login-container">
    <!-- login卡片区域 -->   
    <div class="box-card">  
       <!--头像区域  -->
       <div class="login-tx">
        <img src="@/assets/logo.png"/>
       </div>
       
       <!--登录表单区域  -->
      <el-form  label-width="0px" class="login-form"
      ref="loginFormRef"
      :rules="loginFormRules"
      :model="loginForm"
      >
        <!-- 用户名区域 -->
        <el-form-item prop="username">
          <el-input type="text" placeholder="用户名" suffix-icon="el-icon-user" 
          v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="password">
          <el-input placeholder="密码" show-password 
          v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="login-btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入全局样式表
import '@/assets/css/global.css'

export default {
  data(){
    return{
      loginForm: {
        //用户名
        username:'admin',
        // 密码
        password: '123456',
      },
      
      
      // 表单合法性校验
       loginFormRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
          ],
          password:[
            { required: true, message: '请输入密码', trigger:'blur'},
            { min:5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
          ]
    }
  }
},
  methods: {
    // 重置表单方法
    resetLoginForm(){
      console.log(1);
      this.$refs.loginFormRef.resetFields();
    },
    // 登录预验证方法
    login(){
      this.$refs.loginFormRef.validate( async valid => {
        // console.log(valid);
        // 判断表单数据是否正确
        if(!valid) return;
        // 发起axios请求
        const { data :res} = await this.$http.post("login", this.loginForm);
        if(res.meta.status !== 200) return this.$message.error('登录失败！');
        this.$message({
          message: '登录成功！',
          type: 'success'
        });
        // 登录成功，保存登录的token值到sessionStorage中
        window.sessionStorage.setItem("token", res.data.token);
        // 跳转到home页面
        this.$router.push('/home');
      })
    }
  }
}
</script>


<style lang="less" scoped>
.login-container{
  height: 100%;
  background-color: #2b4b6b;
   .box-card {
    width: 480px;
    height: 380px;
    border-radius: 10px;
    background-color: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    .login-tx{
      img{
        width: 100px;
        height: 100px;
        border-radius: 50%;
      }
      position: absolute;
      left: 50%;
      transform: translate(-50%);
    }
    .login-form{
      box-sizing: border-box;
      padding: 0 20px;
      width: 90%;
      position: absolute;
      bottom: 0;
      .login-btns{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}

</style>
